﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using global::TerhelesEloszto.Engine;

namespace Presentation.Account
{
	public partial class Login : System.Web.UI.Page
	{
		protected int isAdmin = 0;
		bool asTeacher;
		protected global::TerhelesEloszto.Engine.IEngine engine = global::TerhelesEloszto.Engine.Engine.Instance();
		protected void Page_Load( object sender, EventArgs e )
		{

			//RegisterHyperLink.NavigateUrl = "Register.aspx?ReturnUrl=" + HttpUtility.UrlEncode( Request.QueryString["ReturnUrl"] );

		}

		protected void LoginUser_Authenticate( object sender, AuthenticateEventArgs e )
		{
			asTeacher = true;
			if( isAdmin == 1 )
			{
				asTeacher = false;
			}
			else if( isAdmin == 0 )
			{
				asTeacher = true;
			}

				bool accepted = engine.LoginUser( LoginUser.UserName, engine.CreateHash( LoginUser.Password ), asTeacher );
				e.Authenticated = accepted;



		}

		protected void RoleType_SelectedIndexChanged( object sender, EventArgs e )
		{
			System.Web.UI.WebControls.DropDownList role = (System.Web.UI.WebControls.DropDownList)sender;
			isAdmin = role.SelectedIndex;
		}

		protected void LoginUser_LoggedIn( object sender, EventArgs e )
		{
			if( !asTeacher )
			{
				Response.Redirect( "~/Admin/AdminDefault.aspx" );
			}
			else if( asTeacher )
			{
				Response.Redirect( "~/Teacher/TeacherDefault.aspx" );
			}
		}






	}



}
